package com.hrm.company.controller;

import com.hrm.common.controller.BaseController;
import com.hrm.common.entity.Result;
import com.hrm.common.entity.ResultCode;
import com.hrm.company.service.CompanyService;
import com.hrm.company.service.DepartmentService;
import com.hrm.domain.company.Company;
import com.hrm.domain.company.Department;
import com.hrm.domain.company.response.DeptListResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 林海湾Sea
 * @Date :  2021/11/22 17:52
 * @Decription: 企业部门接口（控制器）
 */

@CrossOrigin // 解决跨域
@RestController
@RequestMapping("/company")
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private CompanyService companyService;

    /**
     * 保存部门
     */
    @RequestMapping(value = "/department", method = RequestMethod.POST)
    public Result save(@RequestBody Department department) {
        /**
         * 企业id，目前使用固定值1，后面解决
         */
        department.setCompanyId(companyId);
        // 调用service完成部门保存
        departmentService.save(department);
        // 返回结果值
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 修改部门信息
     */
    @RequestMapping(value = "/department/{id}", method = RequestMethod.PUT)
    public Result update(@PathVariable(value = "id") String id, @RequestBody Department department) {
        // 1、设置修改的部门id
        department.setId(id);
        departmentService.update(department);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id删除部门
     */
    @RequestMapping(value = "/department/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id") String id) {
        departmentService.deleteDepartment(id);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id查找部门信息
     */
    @RequestMapping(value = "/department/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable(value = "id") String id) {
        Department department = departmentService.findById(id);
        return new Result(ResultCode.SUCCESS, department);
    }

    /**
     * 查询企业的部门列表信息
     */
    @RequestMapping(value = "/department", method = RequestMethod.GET)
    public Result findAll() {
        // 1、先指定企业id
        Company company = companyService.findById(companyId);
        // 2、根据企业iD，查询出全部的部门
        List<Department> departmentList = departmentService.findAll(companyId);
        // 3、构造返回结果
        DeptListResult deptListResult = new DeptListResult(company, departmentList);
        return new Result(ResultCode.SUCCESS, deptListResult);
    }
}
